let requestUrl;
let list = [15, 30, 50, 100];

function addHandler() {
	requestUrl = "/equipment_texiao/insert";

	$("#texiao_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#texiao_list").datagrid("getSelected");

	if(rowData) {
		requestUrl = "/equipment_texiao/updateById";

		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#type").combobox("setValue", rowData.type);
		$("#note").textbox("setValue", rowData.note);
		$("#count").numberspinner("setValue", rowData.count);
		$("#propertyId").combobox("setValue", rowData.propertyId);

		$("#texiao_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "info");
	}
}

$(document).ready(function() {

	$("#name").textbox({
		width: 120,
		required: true
	});

	fetchSelect("#type", "/equipment_texiao_type/selectAll", {
		width: 120,
		required: true
	});

	fetchSelect("#propertyId", "/property/selectAll", {
		width: 120,
		required: false
	});

	$("#count").numberspinner({
		min: 1,
		width: 120,
		increment: 1
	});
	
	$("#note").textbox({
		width: 320,
    	height: 100,
    	multiline:true,
    	required:true
	});
	
	$("#texiao_dialog").dialog({
		title: "装备特效",
		width: 440,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#texiao_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(response) {
						showMsg(response.message);

						$(selector).form("clear");
						$("#texiao_dialog").dialog("close");
						$("#texiao_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#texiao_form").form("clear");
				$("#texiao_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr * 15;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_title + heigt_tool + heigt_header + heigt_filter + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	let datagrid = $("#texiao_list").datagrid({
		url: "/equipment_texiao/selectByPage",
		method: "get",
		height: height_table,
		rownumbers: true,
		fitColumns: true,
		pagination: true,
		pageList: list,
		pageSize: list[0],
		remoteFilter: true,
		clientPaging: false,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}],
		columns: [[
			{field: "id", title: "编号", align: "center", hidden: true},
			{field: "name", title: "特效名称", align: "center", width: 80},
			{field: "type", title: "类型", align: "center", width: 80, sortable: true
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/equipment_texiao_type/selectById", {
						id: value
					}, function (res) {
						$("#texiao_type_" + rowIndex).html(res.data.name);
					}, error);

					return "<div id='texiao_type_" + rowIndex + "'></div>";
				}
			},
			{field: "propertyId", title: "增加属性", align: "center", width: 80, sortable: true
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/property/selectById", {
							id: value
						}, function (res) {
							let data = res.data;

							$("#property_" + rowIndex).html(data.name);
						}, error);

						return "<div id='property_" + rowIndex + "'></div>";
					}

					return "<div>/</div>";
				}
			},
			{field: "count", title: "属性值", align: "center", width: 80},
			{field: "note", title: "说明", align: "center", width: 200
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			}
		]]
	});

	datagrid.datagrid("enableFilter", [{
		field: "type",
		type: "combobox",
		options: {
			url: "/equipment_texiao_type/selectAll",
			valueField: "id",
			textField: "name",
			panelHeight: "auto"
		},
		operator: ["equal"]
	}, {
		field: "note",
		type: "textbox",
		operator: ["equal", "contains"]
	}]);

});